﻿using HalconDotNet;
using Halcon二次开发.Controller.CamerForm交互;
using Halcon二次开发.Controller.Controller基类;
using Halcon二次开发.Controller.卡尺;
using Halcon二次开发.Controller.多模板匹配;
using Halcon二次开发.Controller.画线工具交互;
using Halcon二次开发.Controller.精准测量模块功能;
using Halcon二次开发.Controller.结果分析工具;
using Halcon二次开发.Controller.距离转换工具;
using Halcon二次开发.Model;
using Halcon二次开发.Model.储存每个Controller的单例对象;
using Halcon二次开发.Model.储放所有单例.储存每个单例界面;
using Halcon二次开发.Model.帮助类.VHelper;
using Halcon二次开发.Model.自定义控件;
using Halcon二次开发.Tools;
using Halcon二次开发.View.Socket通信;
using Halcon二次开发.View.九点标定;
using Halcon二次开发.View.卡尺;
using Halcon二次开发.View.多文本显示工具;
using Halcon二次开发.View.多模板匹配;
using Halcon二次开发.View.找圆_线_矩形;
using Halcon二次开发.View.文本显示工具;
using Halcon二次开发.View.相机;
using Halcon二次开发.View.精准测量模块;
using Halcon二次开发.View.结果分析工具;
using Halcon二次开发.View.距离转换工具;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Xml.Linq;
using Image = System.Drawing.Image;
using TreeNode = System.Windows.Forms.TreeNode;
using TreeNodeCollection = System.Windows.Forms.TreeNodeCollection;
using TreeView = System.Windows.Forms.TreeView;

namespace Halcon二次开发.Controller.主界面交互
{
    public class Form1Controller : ControllerBase
    {
        public override void Run() { }
        //每个对象的工程文件的路径
        public string filePath = $@"Data\工程文件\";
        //大工程名称
        public string projectName;
        /// <summary>
        /// 将该工程的默认路径进行赋值
        /// </summary>
        /// <param name="name">大工程的名称</param>
        public void SetFilePath(string name)
        {
            filePath += name+"\\";
            projectName = this.name;
        }
        /// <summary>
        /// 用来判断
        /// </summary>
        public string name;
        private bool isMoving = false;
        private Point startPoint;
        int camerIndex, cirleIndex,lineIndex,shapIndex, PreciseCaliperIndex,FixtrueIndex, nodecirleIndex, distanceIndex
            , messageTextIndex, swellEtchIndex, caliperModuleIndex, ninePointIndex,pmasIndex,showTextsIndex,analyseIndex, OCRIndex,socketIndex;
        //让它绑定工位
        public HwindowFlowNodeUrseControl hwindowFlowNode;
        TreeView la1;
        /// <summary>
        /// 鼠标双击事件
        /// </summary>
        public void Btn_Double_Click(HwindowFlowNodeUrseControl hwindow, object tree) 
        {
            this.hwindowFlowNode = hwindow;
            //获取当前板块的所有小工具窗体,工具窗体
            bool check = false;            

            la1 = tree as TreeView;
            if (la1.SelectedNode == null) return;
            string str = la1.SelectedNode.Text;
            Form form = null;
            bool camerCheck = true;
            bool pmaCheck = true;
            foreach (TreeNode item in hwindow.treeView1.Nodes)
            {
                if (item.Text.Contains("相机工具"))
                {
                    camerCheck = false;
                } else if (item.Text.Contains("多目标模型匹配")) {
                    pmaCheck = false;
                }
            }
            if (la1.SelectedNode.Text == "相机工具")
            {
                if (camerCheck)
                {
                    str += 0;
                    form = new CamerForm(hwindowFlowNode);
                    AddHeadNode(form, str, hwindow, la1.SelectedNode.ImageIndex);
                }

            }
            else if (camerCheck)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText("请添加相机工具! \r\n");
            }
            else if (la1.SelectedNode.Text == "找线工具")
            {
                str += lineIndex;
                form = new H_FindLineToolForm(hwindowFlowNode);
                AddHeadNode(form, str, hwindow, la1.SelectedNode.ImageIndex);
            }
            else if (la1.SelectedNode.Text == "找圆工具")
            {
                str += cirleIndex;
                form = new H_FindCirleToolForm(hwindowFlowNode);
                AddHeadNode(form, str, hwindow, la1.SelectedNode.ImageIndex);
            }
            else if (la1.SelectedNode.Text.Contains("文本显示工具"))
            {
                str += messageTextIndex;
                form = new MessageBoxTextForm(hwindowFlowNode);
                AddHeadNode(form, str, hwindow, la1.SelectedNode.ImageIndex);
            }
            else if (la1.SelectedNode.Text.Contains("九点标定"))
            {
                str += ninePointIndex;
                form = new NinePointCalibrationForm(hwindowFlowNode);
                AddHeadNode(form, str, hwindow, la1.SelectedNode.ImageIndex);
            }
            else if (la1.SelectedNode.Text.Contains("多目标模型匹配"))
            {
                if (!pmaCheck) return;
                str += pmasIndex;
                form = new H_PMA_S_AlignToolForm(hwindowFlowNode);
                (form as H_PMA_S_AlignToolForm).SetPathNodes(filePath + hwindowFlowNode.name + "任务文件", str);
                AddHeadNode(form, str, hwindow, la1.SelectedNode.ImageIndex);
            }
            else if (la1.SelectedNode.Text.Contains("测量模块"))
            {
                str += caliperModuleIndex;
                form = new CaliperModuleForm(hwindowFlowNode);

                AddHeadNode(form, str, hwindow, la1.SelectedNode.ImageIndex);
            }else if (la1.SelectedNode.Text.Contains("socket通讯"))
            {
                str += socketIndex;
                form = new SocketChildForm(hwindowFlowNode);
                AddHeadNode(form, str, hwindow, la1.SelectedNode.ImageIndex);
            }
        }
        public void AddHeadNode(Form form,string name, HwindowFlowNodeUrseControl hwindow,int imageIndex)
        {
            this.hwindowFlowNode = hwindow;
            //这里直接传入图层,相机工具除外
            if (!form.Name.Contains("CamerForm") )
            {
                //输入的图层
                object obj = form.GetType().GetField("controller").GetValue(form);
                //输出的图层
                object value = hwindow.treeView1.Nodes[0].Tag.GetType().GetField("controller").GetValue(hwindow.treeView1.Nodes[0].Tag);
                value = value.GetType().GetField("HideOutputImage").GetValue(value);
                if(obj.GetType().GetField("HideIntputImage")!=null)
                {
                    //给输入图层赋值
                    obj.GetType().GetField("HideIntputImage").SetValue(obj, value);
                }
            }
            form.Name = name;
            form.Text = name;
            TreeNode treeNode = new TreeNode(name);
            treeNode.Name = name;
            treeNode.Text = form.Name;

            if (!hwindow.ngDictionary.ContainsKey(treeNode.Name))
            {
                if (treeNode.Name.Contains("模块") || treeNode.Name.Contains("多目标"))
                {
                    hwindow.ngDictionary.Add(treeNode.Name, 0);
                    hwindow.Refresh();
                }
            }
            ContextMenuStrip contex = new ContextMenuStrip();
            //增加一个事件
            contex.Items.Add("删除节点", Image.FromFile("Data\\Image\\Exit2.png"));
            contex.Items[0].Click += Form1Controller_Click;
            contex.Items[0].Tag = treeNode;
            
            //增加一个复制事件
            contex.Items.Add("复制节点", Image.FromFile("Data\\Image\\复制.png"));
            contex.Items[1].Click += Form1Copy_Click1;
            contex.Items[1].Tag = treeNode;

            //增加一个复制事件
            contex.Items.Add("粘贴节点", Image.FromFile("Data\\Image\\粘贴_paste.png"));
            contex.Items[2].Click += Form1Stickup_Click1;
            contex.Items[2].Tag = treeNode;


            treeNode.ContextMenuStrip = contex;
            treeNode.Tag = form;
            form.Tag = treeNode;
            //添加输出子节点
            foreach (var item in form.GetType().GetFields())
            {
                if (item.Name == "controller")
                {
                    object controllerObj = item.GetValue(form);
                    foreach ( var item1  in controllerObj.GetType().GetFields())
                    {
                        ContextMenuStrip contex1 = new ContextMenuStrip();
                        TreeNode childNode = null;
                        bool paper = false;
                        if (item1.Name.Contains("Int") && !item1.Name.Contains("Hide"))
                        {
                            childNode = new TreeNode(item1.Name);
                            childNode.ImageIndex = 75;
                            paper = true;
                            //添加右击菜单 以及 菜单的子菜单
                            AddChildMenuStrip(childNode);
                        }
                        else if (item1.Name.Contains("Out") && !item1.Name.Contains("Hide"))
                        {
                            childNode = new TreeNode(item1.Name);
                            childNode.ImageIndex = 155;
                            paper = true;
                        }
                        else if (item1.Name.Contains("IntOrOut") && !item1.Name.Contains("Hide"))
                        {
                            childNode = new TreeNode(item1.Name);
                            childNode.ImageIndex = 156;
                            paper = true;

                            AddChildMenuStrip(childNode);
                        }
                        if (paper && childNode != null)
                        {
                            childNode.Name = item1.Name;
                            childNode.Tag = controllerObj;
                            treeNode.Nodes.Add(childNode);
                            //多模板匹配的基准数据加载
                            if (form is H_PMA_S_AlignToolForm)
                            {
                                //将我们存储的数据给到Shap_S_Matching
                                string paths = filePath + hwindowFlowNode.name + "工程文件" + "\\" + "基准数据" + "\\" + name + "基准数据" + "\\" + "standar.txt";
                                //判断文件中有没有这个基准数据文件
                                if (File.Exists(paths))
                                {
                                    //多模板基准数据
                                    if (treeNode.Nodes[0].Tag is Shap_S_Matching)
                                    {
                                        Shap_S_Matching s = (Shap_S_Matching)treeNode.Nodes[0].Tag;
                                        List<double> values = HelpJsons.Read<double>(paths);
                                       
                                    }
                                }
                            }
                        }
                    }
                }
            }

            treeNode.ImageIndex = imageIndex;
            //添加节点
            hwindow.treeView1.Nodes.Add(treeNode);
            //同步工具计数器  
            string index = name[name.Length - 1].ToString();//获取节点最后一个字符 

            //这里要获取一下模板匹配的数据,是以当前节点开始找
            int nodeIndex = hwindow.treeView1.Nodes.IndexOf(treeNode);
            TreeNode tree = null;
            for (int i = nodeIndex; i >=0; i--) {
                if (hwindow.treeView1.Nodes[i].Text.Contains("多目标模型匹配"))
                {
                    tree = hwindow.treeView1.Nodes[i];
                }
            }

            if (form is CamerForm)
            {
                camerIndex = int.Parse(index);
                camerIndex++;
            }
            else if (form is H_FindLineToolForm)
            {
                if (tree!=null)
                {
                    //输入的数据
                    FindLineToolController lineToolForm = (form as H_FindLineToolForm).controller;
                    //输出的数据 
                    Shap_S_Matching shapToolForm = (tree.Tag as H_PMA_S_AlignToolForm).controller;

                    lineToolForm.HideIntputArrayRows = shapToolForm.HideOutputArrayRows;
                    lineToolForm.HideIntputArrayColumns = shapToolForm.HideOutputArrayColumns;
                    lineToolForm.HideIntputArrayPhis = shapToolForm.HideOutputArrayPhis;
                }
                

                lineIndex = int.Parse(index);
                lineIndex++;
            }
            else if (form is H_FindCirleToolForm)
            {
                if (tree != null)
                {
                    //输入的数据
                    FindCrileToolController lineToolForm = (form as H_FindCirleToolForm).controller;
                    //输出的数据 
                    Shap_S_Matching shapToolForm = (tree.Tag as H_PMA_S_AlignToolForm).controller;

                    lineToolForm.HideIntputArrayRows = shapToolForm.HideOutputArrayRows;
                    lineToolForm.HideIntputArrayColumns = shapToolForm.HideOutputArrayColumns;
                    lineToolForm.HideIntputArrayPhis = shapToolForm.HideOutputArrayPhis;
                }

                cirleIndex = int.Parse(index);
                cirleIndex++;
            }else if (form is PreciseCaliper)
            {
                if (tree != null)
                {
                    //输入的数据
                    Caliper lineToolForm = (form as PreciseCaliper).controller;
                    //输出的数据 
                    Shap_S_Matching shapToolForm = (tree.Tag as H_PMA_S_AlignToolForm).controller;

                    lineToolForm.HideIntputArrayRows = shapToolForm.HideOutputArrayRows;
                    lineToolForm.HideIntputArrayColumns = shapToolForm.HideOutputArrayColumns;
                    lineToolForm.HideIntputArrayPhis = shapToolForm.HideOutputArrayPhis;
                }
                PreciseCaliperIndex = int.Parse(index);
                PreciseCaliperIndex++;
            }
            else if (form is DistanceToolForm)
            {

                DistanceToolController distanceToolController = (form as DistanceToolForm).controller;
                TreeNode node = hwindow.treeView1.Nodes[nodeIndex - 1];
                if (node != null && (node.Text.Contains("找线工具") || node.Text.Contains("精准测量") || node.Text.Contains("找圆工具")))
                {
                    if (node.Tag is H_FindLineToolForm)
                    {
                        FindLineToolController controller = ((H_FindLineToolForm)node.Tag).controller;
                        distanceToolController.HideIntputArrayDistances = controller.HideOutputArrayDistances;
                    }
                    else if (node.Tag is H_FindCirleToolForm)
                    {
                        FindCrileToolController controller = ((H_FindCirleToolForm)node.Tag).controller;
                        distanceToolController.HideIntputArrayDistances = controller.HideOutputArrayDistances;
                    }
                    else if (node.Tag is PreciseCaliper)
                    {
                        Caliper controller = ((PreciseCaliper)node.Tag).controller;
                        distanceToolController.HideIntputArrayDistances = controller.HideOutputArrayDistances;
                    }
                    else if (node.Tag is DistanceToolForm)
                    {
                        FindLineToolController controller = ((H_FindLineToolForm)node.Tag).controller;
                        distanceToolController.HideIntputArrayDistances = controller.HideOutputArrayDistances;
                    }
                }

                distanceIndex = int.Parse(index);
                distanceIndex++;
            }
            else if (form is MessageBoxTextForm)
            {
                messageTextIndex = int.Parse(index);
                messageTextIndex++;
            }
            else if (form is NinePointCalibrationForm)
            {
                ninePointIndex = int.Parse(index);
                ninePointIndex++;
            } 
            else if (form is H_PMA_S_AlignToolForm) {
                pmasIndex = int.Parse(index);
                pmasIndex++;
            }
            else if (form is AnalyseResultForms)
            {
                AnalyseControl analyseResultForms = (form as AnalyseResultForms).controller;
                TreeNode node = hwindow.treeView1.Nodes[nodeIndex - 1];
                if (node!=null&&(node.Text.Contains("找线工具")|| node.Text.Contains("精准测量")|| node.Text.Contains("找圆工具") || node.Text.Contains("距离转换")))
                {
                    bool paper = false;
                    double[] doubles = new double[0];   
                    if (node.Tag is H_FindLineToolForm)
                    {
                        paper = true;
                        FindLineToolController controller = ((H_FindLineToolForm)node.Tag).controller;
                        doubles = controller.HideOutputArrayDistances;
                    }
                    else if (node.Tag is H_FindCirleToolForm)
                    {
                        paper = true;
                        FindLineToolController controller = ((H_FindLineToolForm)node.Tag).controller;
                        doubles = controller.HideOutputArrayDistances;
                    }
                    else if (node.Tag is PreciseCaliper)
                    {
                        paper = true;
                        FindLineToolController controller = ((H_FindLineToolForm)node.Tag).controller;
                        doubles = controller.HideOutputArrayDistances;
                    }
                    else if (node.Tag is DistanceToolForm) {
                        paper = true;
                        DistanceToolController controller = ((DistanceToolForm)node.Tag).controller;

                        doubles = controller.HideOutputArrayDistances;
                    }
                    if (paper)
                    {
                        if (doubles != null)
                        {
                            analyseResultForms.HideIntputArrays.AddRange(doubles);
                        }
                    }
                }
                analyseIndex=int.Parse(index);
                analyseIndex++;
            }else if (form is SocketChildForm)
            {
                socketIndex = int.Parse(index);
                socketIndex++;
            }
            else if (form is MessageBoxStringsForms)
            {
                showTextsIndex = int.Parse(index);
                showTextsIndex++;
            }else if (form is CaliperModuleForm)
            {
                (form as CaliperModuleForm).LoadAntherForm();
                if (tree != null)
                {
                    CaliperModuleController  cmdController = (form as CaliperModuleForm).controller;
                    //输出的数据 
                    Shap_S_Matching shapToolForm = (tree.Tag as H_PMA_S_AlignToolForm).controller;
                    cmdController.HideIntputArrayRows = shapToolForm.HideOutputArrayRows;
                    cmdController.HideIntputArrayColumns = shapToolForm.HideOutputArrayColumns;
                    cmdController.HideIntputArrayPhis = shapToolForm.HideOutputArrayPhis;
                    cmdController.HideIntputImage = shapToolForm.HideIntputImage;
                    cmdController.DataTransfer();
                }
                caliperModuleIndex = int.Parse(index);
                caliperModuleIndex++;
            }
        }

        /// <summary>
        /// 粘贴事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Form1Stickup_Click1(object sender, EventArgs e)
        {
            if (StaicResouce.CopyTreeNode == null)
            {
                MessageBox.Show("当前无节点!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            ToolStripItem view = sender as ToolStripItem;
            TreeView treeView = null;
            TreeNode node = view.Tag as TreeNode;
            if (node != null)
            {
                treeView = node.TreeView;
            }
            else
            {
                treeView = view.Tag as TreeView;
            }
            //这里要更新窗体句柄
            StaicResouce.CopyTreeNode.Tag.GetType().GetMethod("PushHWindon").Invoke(StaicResouce.CopyTreeNode.Tag,new object[] { hwindowFlowNode });

            
            if (StaicResouce.CopyTreeNode==null)
            {
                MessageBox.Show("当前无节点!","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }
            string nodestr = "";
            if (StaicResouce.CopyTreeNode.Text.Contains("相机工具"))
            {
                foreach (TreeNode item in treeView.Nodes)
                {
                    if (item.Text.Contains("相机工具"))
                    {
                        StaicResouce.CopyTreeNode = null;
                        MessageBox.Show("当前工程已有相机工具!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
                StaicResouce.CopyTreeNode.Text = "相机工具" + 0;
                StaicResouce.CopyTreeNode.Name = "相机工具" + 0;
            }
            else if (StaicResouce.CopyTreeNode.Text.Contains("找线工具"))
            {
                lineIndex++;
                StaicResouce.CopyTreeNode.Text = "找线工具" + lineIndex;
                StaicResouce.CopyTreeNode.Name = "找线工具" + lineIndex;
                ((Form)StaicResouce.CopyTreeNode.Tag).Text = "找线工具" + lineIndex;
                ((Form)StaicResouce.CopyTreeNode.Tag).Name = "找线工具" + lineIndex;
            }
            else if (StaicResouce.CopyTreeNode.Text.Contains("找圆工具"))
            {
                cirleIndex++;
                StaicResouce.CopyTreeNode.Text = "找圆工具" + cirleIndex;
                StaicResouce.CopyTreeNode.Name = "找圆工具" + cirleIndex;
                ((Form)StaicResouce.CopyTreeNode.Tag).Text = "找圆工具" + cirleIndex;
                ((Form)StaicResouce.CopyTreeNode.Tag).Name = "找圆工具" + cirleIndex;
            }
            else if (StaicResouce.CopyTreeNode.Text.Contains("文本显示工具"))
            {
                messageTextIndex++;
                StaicResouce.CopyTreeNode.Text = "文本显示工具" + messageTextIndex;
                StaicResouce.CopyTreeNode.Name = "文本显示工具" + messageTextIndex;
                ((Form)StaicResouce.CopyTreeNode.Tag).Text = "文本显示工具" + messageTextIndex;
                ((Form)StaicResouce.CopyTreeNode.Tag).Name = "文本显示工具" + messageTextIndex;
            }
            else if (StaicResouce.CopyTreeNode.Text.Contains("九点标定"))
            {
                ninePointIndex++;
                StaicResouce.CopyTreeNode.Text = "九点标定" + ninePointIndex;
                StaicResouce.CopyTreeNode.Name = "九点标定" + ninePointIndex;
                ((Form)StaicResouce.CopyTreeNode.Tag).Text = "九点标定" + ninePointIndex;
                ((Form)StaicResouce.CopyTreeNode.Tag).Name = "九点标定" + ninePointIndex;
            }
            else if (StaicResouce.CopyTreeNode.Text.Contains("多目标模型匹配"))
            {
                pmasIndex++;
                StaicResouce.CopyTreeNode.Text = "多目标模型匹配" + pmasIndex;
                StaicResouce.CopyTreeNode.Name = "多目标模型匹配" + pmasIndex;
                ((Form)StaicResouce.CopyTreeNode.Tag).Text = "多目标模型匹配" + pmasIndex;
                ((Form)StaicResouce.CopyTreeNode.Tag).Name = "多目标模型匹配" + pmasIndex;
            }
            else if (StaicResouce.CopyTreeNode.Text.Contains("测量模块"))
            {
                caliperModuleIndex++;
                StaicResouce.CopyTreeNode.Text = "测量模块工具" + caliperModuleIndex;
                StaicResouce.CopyTreeNode.Name = "测量模块工具" + caliperModuleIndex;
                ((Form)StaicResouce.CopyTreeNode.Tag).Text = "测量模块工具" + caliperModuleIndex;
                ((Form)StaicResouce.CopyTreeNode.Tag).Name = "测量模块工具" + caliperModuleIndex;
            }
            else if (StaicResouce.CopyTreeNode.Text.Contains("socket通讯"))
            {
                socketIndex++;
                StaicResouce.CopyTreeNode.Text = "socket通讯" + socketIndex;
                StaicResouce.CopyTreeNode.Name = "socket通讯" + socketIndex;
                ((Form)StaicResouce.CopyTreeNode.Tag).Text = "socket通讯" + socketIndex;
                ((Form)StaicResouce.CopyTreeNode.Tag).Name = "socket通讯" + socketIndex;
            }

            //给复制的节点也绑定一个删除-粘贴-复制事件
            ContextMenuStrip contex = new ContextMenuStrip();
            //增加一个事件
            contex.Items.Add("删除节点", Image.FromFile("Data\\Image\\Exit2.png"));
            contex.Items[0].Click += Form1Controller_Click;
            contex.Items[0].Tag = StaicResouce.CopyTreeNode;

            //增加一个复制事件
            contex.Items.Add("复制节点", Image.FromFile("Data\\Image\\复制.png"));
            contex.Items[1].Click += Form1Copy_Click1;
            contex.Items[1].Tag = StaicResouce.CopyTreeNode;

            //增加一个复制事件
            contex.Items.Add("粘贴节点", Image.FromFile("Data\\Image\\粘贴_paste.png"));
            contex.Items[2].Click += Form1Stickup_Click1;
            contex.Items[2].Tag = StaicResouce.CopyTreeNode;

            StaicResouce.CopyTreeNode.ContextMenuStrip = contex;
            if (!hwindowFlowNode.ngDictionary.ContainsKey(StaicResouce.CopyTreeNode.Name))
            {
                hwindowFlowNode.ngDictionary.Add(StaicResouce.CopyTreeNode.Name, 0);
                hwindowFlowNode.Refresh();
            }
            //这里要增加节点进去
            treeView.Nodes.Add(StaicResouce.CopyTreeNode);
            StaicResouce.CopyTreeNode = null;
        }

        /// <summary>
        /// 复制节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Form1Copy_Click1(object sender, EventArgs e)
        {
            //先实例化节点
            StaicResouce.CopyTreeNode = new TreeNode();

            ToolStripItem item =  sender as ToolStripItem;
            //选择复制的节点
            TreeNode node = item.Tag as TreeNode;

            //判断下标
            if (node.Text.Contains("相机工具")) {
                StaicResouce.CopyTreeNode.Text = "相机工具" + 0;
                StaicResouce.CopyTreeNode.Name = "相机工具" + 0;
            }
            else if (node.Text.Contains("找线工具"))
            {
                StaicResouce.CopyTreeNode.Text = "找线工具" + lineIndex;
                StaicResouce.CopyTreeNode.Name = "找线工具" + lineIndex;
            }
            else if (node.Text.Contains("找圆工具"))
            {
                StaicResouce.CopyTreeNode.Text = "找圆工具" + cirleIndex;
                StaicResouce.CopyTreeNode.Name = "找圆工具" + cirleIndex;
            }
            else if (node.Text.Contains("文本显示工具"))
            {
                StaicResouce.CopyTreeNode.Text = "文本显示工具" + messageTextIndex;
                StaicResouce.CopyTreeNode.Name = "文本显示工具" + messageTextIndex;
            }
            else if (node.Text.Contains("九点标定"))
            {
                StaicResouce.CopyTreeNode.Text = "九点标定" + ninePointIndex;
                StaicResouce.CopyTreeNode.Name = "九点标定" + ninePointIndex;
            }
            else if (node.Text.Contains("多目标模型匹配"))
            {
                StaicResouce.CopyTreeNode.Text = "多目标模型匹配" + pmasIndex;
                StaicResouce.CopyTreeNode.Name = "多目标模型匹配" + pmasIndex;
            }
            else if (node.Text.Contains("测量模块"))
            {
                StaicResouce.CopyTreeNode.Text = "测量模块工具" + caliperModuleIndex;
                StaicResouce.CopyTreeNode.Name = "测量模块工具" + caliperModuleIndex;
            }
            else if (node.Text.Contains("socket通讯"))
            {
                StaicResouce.CopyTreeNode.Text = "socket通讯" + socketIndex;
                StaicResouce.CopyTreeNode.Name = "socket通讯" + socketIndex;
            }

            //这里要获得Image
            StaicResouce.CopyTreeNode.ImageIndex = node.ImageIndex;
            //这里要实例获取到的对象
            (node.Tag as Form).Name = node.Text;
            object obj =  Activator.CreateInstance(node.Tag.GetType(),new object[] { hwindowFlowNode});



            //这里要赋值名称给窗体,后台也是加载这个名字
            ((Form)obj).Text = StaicResouce.CopyTreeNode.Text;
            ((Form)obj).Name = StaicResouce.CopyTreeNode.Text;
            //这里加载模块工具 , 不是模块的直接跳过即可
            if (obj.GetType().GetField("ParentPanl")!=null)
            {
                obj.GetType().GetMethod("LoadAntherForm").Invoke(obj, null);
                FieldInfo[] fieldInfos = obj.GetType().GetFields();
                
                TabControl tabControl = (TabControl)obj.GetType().GetField("ParentPanl").GetValue(obj);
                int pageCount = 0;
                
                foreach (var field in fieldInfos)
                {
                    if (field.Name.Contains("Form"))
                    {
                        //转载子窗体
                        Form form = (Form)field.GetValue(obj);
                        TabPage tabPage = new TabPage();
                        Utiy.AddPage(form, tabControl.TabPages[pageCount]); 
                        pageCount++;
                    }
                }
                foreach (TabPage sss in tabControl.TabPages)
                {
                    Console.WriteLine(sss.Controls[0].Text);
                }
            }
            //这里是存入数据,共同点就是Tag,说白了此时的Tag就是一个Form,因此需要一个递归
            //这里要获得被复制的窗体对象
            Form oldForm = (Form)node.Tag;
            Form newForm = (Form)obj;


            for (int i = 0; i < oldForm.Controls.Count; i++)
            {
                AddChildControl(newForm.Controls[i].Controls, oldForm.Controls[i].Controls);
            }

            //绑定节点
            StaicResouce.CopyTreeNode.Tag = obj;
        }
        /// <summary>
        /// 递归
        /// </summary>
        /// <param name="newControl"></param>
        /// <param name="oldControl"></param>
        void AddChildControl(System.Windows.Forms.Control.ControlCollection newControl, System.Windows.Forms.Control.ControlCollection oldControl) {
            for (int i =0;i<newControl.Count;i++) {
                if (newControl[i].Tag!=null)
                {
                    newControl[i].Text = oldControl[i].Text;
                }
                AddChildControl(newControl[i].Controls, oldControl[i].Controls);
            }
        }

        /// <summary>
        /// 初始化右击菜单栏
        /// </summary>
        /// <param name="childNode"></param>
        public void AddChildMenuStrip(TreeNode childNode)
        {
            ContextMenuStrip context = new ContextMenuStrip();
            ToolStripMenuItem toolStrip1 = new ToolStripMenuItem("连接至");
            ToolStripMenuItem toolStrip2 = new ToolStripMenuItem("取消连接");
            context.Items.Add(toolStrip1);
            context.Items.Add(toolStrip2);
            context.Items[1].Tag = childNode;
            childNode.ContextMenuStrip = context;
            toolStrip2.Click += ToolStrip2_Click;
        }
        /// <summary>
        /// Int/IntOrOut 取消连接操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStrip2_Click(object sender, EventArgs e)
        {
             object obj= (sender as ToolStripMenuItem).Tag;
            if (obj != null)
            {
                TreeNode node=(sender as ToolStripMenuItem).Tag as TreeNode;
                string str = node.Text.Substring(0,node.Text.IndexOf('<'));
                //获取这个实例
                object form = node.Parent.Tag.GetType().GetField("controller").GetValue(node.Parent.Tag);
                //获取这个字段对象
                FieldInfo value = node.Parent.Tag.GetType().GetField("controller").GetValue(node.Parent.Tag).GetType().GetField(str);
                
                if (value.GetType().IsPrimitive)
                {
                    //基本数据类型
                    value.SetValue(form, 0);
                }
                else {
                    string xx = form.GetType().Name;
                    //对象类型
                    value.SetValue(form, null );   
                }
                if (node.Text != node.Name)
                {
                    node.Text = node.Name;
                    node.ForeColor = Color.Black;
                }else
                {
                    return;
                }

            }
        }
        /// <summary>
        /// 删除节点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1Controller_Click(object sender, EventArgs e)
        {
            try
            {
                ToolStripMenuItem x = (ToolStripMenuItem)sender;
                TreeNode node = (TreeNode)x.Tag;
                foreach (var item in StaicResouce.HomeForms)
                {
                    HwindowFlowNodeUrseControl sss = item as HwindowFlowNodeUrseControl;
                    if (sss.name == name)
                    {
                        TreeView treeView = sss.treeView1;
                        string sssss = node.Text;
                        //清理使用此节点输出端的子节点
                        int index = treeView.Nodes.IndexOf(node);
                        for (int i = index; i < treeView.Nodes.Count; i++)// 循环根节点
                        {
                            //释放socket工具的输入段子
                            // node.Text （核心） 删除的节点文本
                            if (treeView.Nodes[i].Tag is SocketChildForm)
                            {
                                TreeView view = (treeView.Nodes[i].Tag as SocketChildForm).treeView1;
                                foreach (TreeNode childNode in view.Nodes[0].Nodes)
                                {
                                    if (childNode.Text.Contains(node.Text.Trim()))
                                    {
                                        childNode.ForeColor = Color.Black;
                                        childNode.Text = childNode.Name;
                                    }
                                }
                                continue;
                            }

                            TreeNodeCollection childNodes = treeView.Nodes[i].Nodes;
                            foreach (TreeNode childNode in childNodes)// 循环子节点
                            {
                                string[] childNodeText = childNode.Text.Split(new char[] { '<', '—' }, StringSplitOptions.RemoveEmptyEntries);
                                if (childNodeText.Length > 1)
                                {
                                    string str = childNodeText[1].Split('.')[0];
                                    if (str == node.Text)
                                    {
                                        childNode.Text = childNode.Name;
                                        childNode.ForeColor = Color.Black;
                                    }
                                }
                            }

                        }
                        //释放绘制的区域
                        if (node.Nodes.Count != 0)
                        {
                            object obj = node.Nodes[0].Tag;
                            if (obj is FindCrileToolController || obj is FindLineToolController || obj is Caliper)
                            {
                                MethodInfo method = obj.GetType().GetMethod("ClearObject");
                                method.Invoke(obj, null);
                            }
                        }

                        //释放窗体
                        if (node.Tag is Form)
                        {
                            //删除相机的图片资源
                            if (node.Text.Equals("相机工具0"))
                            {
                                CamerForm camerForm = node.Tag as CamerForm;
                                if (camerForm != null)
                                {
                                    try
                                    {
                                        camerForm.controller.Paths = new List<string>();
                                        HelpJsons.Write<string>(camerForm.controller.Paths, filePath + hwindowFlowNode.name + "工程文件\\相机图片数据\\相机工具0.json");
                                    }
                                    catch (Exception ex) { Console.WriteLine("Form1Controller类中的472行报错了"); }
                                }
                            }
                            //释放非静态的
                            (node.Tag as Form).Close();
                            (node.Tag as Form).Dispose();
                        }

                        //删除柱状图的数据
                        hwindowFlowNode.ngDictionary.Remove(node.Name);
                        hwindowFlowNode.Refresh();

                        //彻底删除该节点
                        treeView.Nodes.Remove(node);

                        //释放节点占有的文件
                    }
                }
            }
            catch
            {

            }
        }
        /// <summary>`
        /// 打开窗体
        /// </summary>
        /// <param name="sender"></param>
        public void ShowForm(object obj) {
            if (obj != null && obj is Form)
            {
                //窗体自动获得焦点
                (obj as Form).BringToFront();
                (obj as Form).Focus();
                (obj as Form).Show();
            }
        }
    }
}
